using Gameplay.PVE.Entity;
using UnityEngine;

namespace Gameplay.PVE.Skill
{
    public class TrackMover : SkillInitiativeMover
    {
        private int state;
        private float skillRange = 1.5f;
        private UnitBase target;
        private float stateStartTime;
        private Vector3 startPosition;

        public override void Start(Vector3 startPosition,Vector3 endPosition)
        {
            base.Start(startPosition,endPosition);
            this.startPosition = startPosition;
            state = 0;
            unit.Data.attackState = 0;
            moveFinished = false;
            backFinished = false;
            this.target = skill.targetUnit;
            unit.Data.moveState = skill.config.move_state;
            skillRange = 0;//skill.config.cast_range;
            /*if (skillRange < 0.1f)
            {
                skillRange = 0.1f;
            }*/
            unit.Transmit(ETransmitType.ShowGhostShadow);
        }
        
        public override void Update()
        {
            base.Update();
            if (state == 0)
            {
                var targetPosition = skill.targetPosition;
                var diff = targetPosition - unit.Data.position;
                var speed = skill.config.move_speed * TimeManager.LogicDeltaTime;
                if (diff.sqrMagnitude <= skillRange * skillRange || diff.sqrMagnitude <= speed)
                {
                    unit.Data.attackState = 1;
                    unit.Data.manualForward = Vector3.zero;
                    skill.DoStartCast();
                    unit.Play(skill.config.action_name);
                    moveFinished = true;
                    backFinished = false;
                    unit.Transmit(ETransmitType.HideGhostShadow);
                    state = 1;
                    stateStartTime = TimeManager.logicTime;
                }
                else
                {
                    var forward =  diff.normalized;
                    unit.Data.manualForward = forward;
                    unit.Data.position += forward * speed;
                    unit.Data.forward = forward;
                }
            }
            else if (state == 1)
            {
                skill.DoCastEvent();
                if (TimeManager.logicTime - stateStartTime > skill.config.attack_cast_time)
                {
                    state = 2;
                    unit.Data.attackState = 0;
                    unit.Data.moveState = skill.config.move_state;
                    unit.Transmit(ETransmitType.ShowGhostShadow);
                }
            }
            else if (state == 2)
            {
                var targetPosition = startPosition;
                var diff = targetPosition - unit.Data.position;
                var speed = skill.config.move_speed * TimeManager.LogicDeltaTime;
                if (diff.sqrMagnitude <= skillRange * skillRange || diff.sqrMagnitude <= speed)
                {
                    unit.Data.manualForward = Vector3.zero;
                    unit.Data.moveState = 0;
                    unit.Data.position = targetPosition;
                    moveFinished = true;
                    backFinished = true;
                    unit.Transmit(ETransmitType.HideGhostShadow);
                }
                else
                {
                    var forward =  diff.normalized;
                    unit.Data.manualForward = forward;
                    unit.Data.position += forward * speed;
                    unit.Data.forward = forward;
                }
            }
        }

        public override void End()
        {
            unit.Data.manualForward = Vector3.zero;
            base.End();
        }        
    }
}